'use strict';

const titbit = require('../main');
const cluster = require('cluster');

var app = new titbit({
  debug : true,
  allow : [
    '127.0.0.1'
  ],
  maxIPRequest: 10,
  peerTime: 5,
  useLimit: true,
  http2: true,
  cert : './rsa/localhost-cert.pem',
  key : './rsa/localhost-privkey.pem',
  notFound : `<!DOCTYPE html><html>
    <head>
    <meta charset="utf-8">
    <title>404</title>
    </head>
    <body>
      <div style="text-align:center;font-size:185%;color:#696969;margin:1.2rem;">
        404 : 未发现资源
      </div>
    </body>
    </html>`
});

app.pre(async (c, next) => {
  console.log('route path:', c.routepath, c.method);
  await next();
});

app.pre(async (c, next) => {
  console.log('set body size');
  
  c.maxBody = 24;

  await next();
}, (ctx, name='') => {
  if (ctx.method !== 'POST') {
    return false;
  }
}, 'post-data');


app.get('/', async c => {
  c.send(`${app.whoami} ok`);
});

app.get('/test', async c => {
  c.res.body = c.name;
}, 'test');

app.post('/test', async c => {
  c.res.body = c.body;
}, {name:'test-post', group: 'post-data'});

app.use(async (c, next) => {
  console.log('POST AND GET');
  await next();
}, 'post-get');

app.map(['GET','POST'], '/gp', async c => {
  let data = {
    method : c.method,
    body : c.body
  };
  c.send(data);
}, '@post-get');

app.printRoute();

app.daemon(2000, 2);
